/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SES100.java
 *
 * Created on 2011-7-5, 13:21:55
 */
package seso;

import java.awt.Graphics;
import javax.swing.JLabel;


import java.awt.Color;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import org.jdesktop.application.Task;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.beansbinding.AbstractBindingListener;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.PropertyStateEvent;
import javax.swing.*;
import java.awt.*;

import java.sql.*;

/**
 *
 * @author Administrator
 */
//public class SES100 extends javax.swing.JDialog {
public class MMS001B extends MaintDialog {

    private JLabel[] headerJLabel;
    private HeaderJSeparator[] headerJSeparator;
    private int headerXAdjust = 1;
    private boolean isFirstTime = true;
    private Color tableBackgroundColor = new Color(235, 238, 241);
    
    String mmitno = "1161150660";
    //mitmasQuery.setParameter("mmitno", mmitno);
    
    /** Creates new form SES130 */
    public MMS001B(javax.swing.JFrame parent, boolean modal) {
        super(parent, modal);
        
        initComponents();
        isBrowseDialog = true;
        refreshToolsBar();

        setProgTitle("MMS001/B*", "Item. Open");

        initTableHeader();
    }

    private void initTableHeader() {
        headerJLabel = new JLabel[masterTable.getColumnCount()];
        headerJSeparator = new HeaderJSeparator[masterTable.getColumnCount()];

        for (int i = 0; i < masterTable.getColumnCount(); i++) {
            headerJLabel[i] = new JLabel();
            headerJLabel[i].setForeground(Color.WHITE);
            headerJLabel[i].setFont(new Font("Arial", Font.BOLD, 11));
            headerJLabel[i].setText(masterTable.getColumnModel().getColumn(i).getHeaderValue().toString());

            if (headerJLabel[i].getText().equals("Customer Id")) {
                headerJLabel[i].setHorizontalAlignment(JLabel.RIGHT);
            } else {
                headerJLabel[i].setHorizontalAlignment(JLabel.LEFT);
            }
            masterHeader.add(headerJLabel[i]);

            headerJSeparator[i] = new HeaderJSeparator();
            headerJSeparator[i].setForeground(new Color(150, 170, 190));
            headerJSeparator[i].setJTable(masterTable);
            headerJSeparator[i].setIndex(i);

            headerJSeparator[i].setOrientation(SwingConstants.VERTICAL);
            //headerJSeparator[i].setBackground(new Color(204,204,255));
            headerJSeparator[i].setBackground(new Color(192, 192, 192));
            headerJSeparator[i].setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
            masterHeader.add(headerJSeparator[i]);
        }


    }

    private void RefreshTableHeader() {
        int startPosition = 0;
        int width = 0;
        for (int i = 0; i < masterTable.getColumnCount(); i++) {
            width = this.masterTable.getColumnModel().getColumn(i).getWidth();
            headerJLabel[i].setBounds(headerXAdjust + startPosition + 5, 10, width - 8, 20);

            startPosition = startPosition + width;
            headerJSeparator[i].setBounds(headerXAdjust + startPosition - 1, 8, 2, masterHeader.getHeight() - 10);
        }
        
        fldMMITNO.setSize(masterTable.getColumnModel().getColumn(0).getWidth() - fldMMITNO.getX() * 3, fldMMITNO.getHeight());
        
        fldMMITTY.setLocation(masterTable.getColumnModel().getColumn(0).getWidth() + 3, fldMMITTY.getY());
        fldMMITTY.setSize(masterTable.getColumnModel().getColumn(1).getWidth() - 9, fldMMITTY.getHeight());
    }

    @Override
    public void paint(Graphics g) {

        MoreSetControlsLocationAndSize();
        RefreshTableHeader();

        super.paint(g);
    }

    private void MoreSetControlsLocationAndSize() {

        int width = getWidth(), height = getHeight();

        masterJScrollPane.setBackground(tableBackgroundColor);
        masterJScrollPane.getViewport().setBackground(tableBackgroundColor);
        masterTable.setBackground(tableBackgroundColor);

        rDlgDataAeraJPanel.setSize(width, height - 90); //  ToolsBar and StatusBar total margin

        rDlgTableHeaderBackground.setSize(rDlgDataAeraJPanel.getWidth() - rDlgTableHeaderBackground.getX() * 2, rDlgTableHeaderBackground.getHeight());

        int tableBottomMargin = 80; // bottom margin
        masterJScrollPane.setLocation(rDlgTableHeaderBackground.getX() + 1, rDlgTableHeaderBackground.getY() + rDlgTableHeaderBackground.getHeight());
        masterJScrollPane.setSize(rDlgTableHeaderBackground.getWidth() - 2, rDlgDataAeraJPanel.getHeight() - masterJScrollPane.getY() - tableBottomMargin);
        masterTable.setSize(masterJScrollPane.getWidth(), masterJScrollPane.getHeight());

        masterHeader.setLocation(rDlgTableHeaderBackground.getX(), rDlgTableHeaderBackground.getY());
        masterHeader.setSize(rDlgTableHeaderBackground.getWidth(), rDlgTableHeaderBackground.getHeight());

        bottomNavJPanel.setLocation(10, rDlgDataAeraJPanel.getHeight() - 10 - bottomNavJPanel.getHeight());
        bottomNavJPanel.setSize(rDlgDataAeraJPanel.getWidth() - bottomNavJPanel.getX() * 2, bottomNavJPanel.getHeight());

        comNavClose.setLocation(0, 0);
        comNavNext.setLocation(bottomNavJPanel.getWidth() - comNavNext.getWidth(), 0);
        comNavPrev.setLocation(comNavNext.getX() - comNavPrev.getWidth(), 0);

        createSKURButton.setLocation(masterJScrollPane.getX() + masterJScrollPane.getWidth() - createSKURButton.getWidth(), createSKURButton.getY());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("AS805;databaseName=M3BETESTPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(seso.SesoApp.class).getContext().getResourceMap(MMS001B.class);
        mitmasQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("mitmasQuery.query")).setMaxResults(200); // NOI18N
        mitmasList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(mitmasQuery.getResultList());
        rDlgDataAeraJPanel = new seso.RDialogDataAeraPanel();
        masterHeader = new seso.RPanel();
        fldMMITNO = new seso.RTextField();
        fldMMITTY = new seso.RTextField();
        rDlgTableHeaderBackground = new seso.RTableHeaderBackground();
        masterJScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        bottomNavJPanel = new javax.swing.JPanel();
        comNavClose = new javax.swing.JButton();
        comNavNext = new javax.swing.JButton();
        comNavPrev = new javax.swing.JButton();
        createSKURButton = new seso.RBgButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        getContentPane().setLayout(null);

        rDlgDataAeraJPanel.setName("rDlgDataAeraJPanel"); // NOI18N
        rDlgDataAeraJPanel.setLayout(null);

        masterHeader.setBorder(null);
        masterHeader.setName("masterHeader"); // NOI18N
        masterHeader.setLayout(null);

        fldMMITNO.setText(resourceMap.getString("fldMMITNO.text")); // NOI18N
        fldMMITNO.setName("fldMMITNO"); // NOI18N
        masterHeader.add(fldMMITNO);
        fldMMITNO.setBounds(3, 27, 110, 24);

        fldMMITTY.setText(resourceMap.getString("fldMMITTY.text")); // NOI18N
        fldMMITTY.setName("fldMMITTY"); // NOI18N
        masterHeader.add(fldMMITTY);
        fldMMITTY.setBounds(140, 27, 90, 24);

        rDlgDataAeraJPanel.add(masterHeader);
        masterHeader.setBounds(20, 10, 330, 52);

        rDlgTableHeaderBackground.setName("rDlgTableHeaderBackground"); // NOI18N
        rDlgDataAeraJPanel.add(rDlgTableHeaderBackground);
        rDlgTableHeaderBackground.setBounds(10, 50, 820, 52);

        masterJScrollPane.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        masterJScrollPane.setName("masterJScrollPane"); // NOI18N

        masterTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        masterTable.setFillsViewportHeight(true);
        masterTable.setName("masterTable"); // NOI18N
        masterTable.setRowHeight(20);
        masterTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        masterTable.setTableHeader(null);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, mitmasList, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mmitno}"));
        columnBinding.setColumnName("Mmitno");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mmitty}"));
        columnBinding.setColumnName("Mmitty");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mmitds}"));
        columnBinding.setColumnName("Mmitds");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mmstat}"));
        columnBinding.setColumnName("Mmstat");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mmgrti}"));
        columnBinding.setColumnName("Mmgrti");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterTable.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                masterTableComponentMoved(evt);
            }
            public void componentResized(java.awt.event.ComponentEvent evt) {
                masterTableComponentResized(evt);
            }
        });
        masterJScrollPane.setViewportView(masterTable);
        masterTable.getColumnModel().getColumn(0).setPreferredWidth(150);
        masterTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("masterTable.columnModel.title0")); // NOI18N
        masterTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        masterTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("masterTable.columnModel.title1")); // NOI18N
        masterTable.getColumnModel().getColumn(2).setPreferredWidth(250);
        masterTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("masterTable.columnModel.title2")); // NOI18N
        masterTable.getColumnModel().getColumn(3).setPreferredWidth(100);
        masterTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("masterTable.columnModel.title3")); // NOI18N
        masterTable.getColumnModel().getColumn(4).setPreferredWidth(100);
        masterTable.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("masterTable.columnModel.title4")); // NOI18N

        rDlgDataAeraJPanel.add(masterJScrollPane);
        masterJScrollPane.setBounds(10, 110, 970, 90);

        jLabel1.setName("jLabel1"); // NOI18N
        rDlgDataAeraJPanel.add(jLabel1);
        jLabel1.setBounds(12, 30, 80, 15);

        bottomNavJPanel.setName("bottomNavJPanel"); // NOI18N
        bottomNavJPanel.setOpaque(false);
        bottomNavJPanel.setLayout(null);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(seso.SesoApp.class).getContext().getActionMap(MMS001B.class, this);
        comNavClose.setAction(actionMap.get("comNavCloseAction")); // NOI18N
        comNavClose.setIcon(resourceMap.getIcon("comNavClose.icon")); // NOI18N
        comNavClose.setBorder(null);
        comNavClose.setBorderPainted(false);
        comNavClose.setContentAreaFilled(false);
        comNavClose.setFocusPainted(false);
        comNavClose.setFocusable(false);
        comNavClose.setName("comNavClose"); // NOI18N
        comNavClose.setPressedIcon(resourceMap.getIcon("comNavClose.pressedIcon")); // NOI18N
        comNavClose.setRolloverIcon(resourceMap.getIcon("comNavClose.rolloverIcon")); // NOI18N
        bottomNavJPanel.add(comNavClose);
        comNavClose.setBounds(0, 0, 88, 28);

        comNavNext.setIcon(resourceMap.getIcon("comNavNext.icon")); // NOI18N
        comNavNext.setBorder(null);
        comNavNext.setBorderPainted(false);
        comNavNext.setContentAreaFilled(false);
        comNavNext.setFocusPainted(false);
        comNavNext.setFocusable(false);
        comNavNext.setName("comNavNext"); // NOI18N
        comNavNext.setPressedIcon(resourceMap.getIcon("comNavNext.pressedIcon")); // NOI18N
        comNavNext.setRolloverIcon(resourceMap.getIcon("comNavNext.rolloverIcon")); // NOI18N
        bottomNavJPanel.add(comNavNext);
        comNavNext.setBounds(587, 0, 88, 28);

        comNavPrev.setIcon(resourceMap.getIcon("comNavPrev.icon")); // NOI18N
        comNavPrev.setBorder(null);
        comNavPrev.setBorderPainted(false);
        comNavPrev.setContentAreaFilled(false);
        comNavPrev.setFocusPainted(false);
        comNavPrev.setFocusable(false);
        comNavPrev.setName("comNavPrev"); // NOI18N
        comNavPrev.setPressedIcon(resourceMap.getIcon("comNavPrev.pressedIcon")); // NOI18N
        comNavPrev.setRolloverIcon(resourceMap.getIcon("comNavPrev.rolloverIcon")); // NOI18N
        bottomNavJPanel.add(comNavPrev);
        comNavPrev.setBounds(530, 0, 54, 28);

        rDlgDataAeraJPanel.add(bottomNavJPanel);
        bottomNavJPanel.setBounds(10, 270, 670, 28);

        createSKURButton.setText(resourceMap.getString("createSKURButton.text")); // NOI18N
        createSKURButton.setFocusable(false);
        createSKURButton.setName("createSKURButton"); // NOI18N
        rDlgDataAeraJPanel.add(createSKURButton);
        createSKURButton.setBounds(710, 20, 120, 28);

        getContentPane().add(rDlgDataAeraJPanel);
        rDlgDataAeraJPanel.setBounds(0, 71, 1000, 330);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void masterTableComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_masterTableComponentResized
        RefreshTableHeader();
    }//GEN-LAST:event_masterTableComponentResized

    private void masterTableComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_masterTableComponentMoved
        String paramString = evt.paramString();
        int indexStart = paramString.indexOf("(") + 1;
        int indexEnd = paramString.indexOf(",");

        int newHeaderXAdjust = Integer.parseInt(paramString.substring(indexStart, indexEnd));
        if (newHeaderXAdjust != headerXAdjust) {
            headerXAdjust = newHeaderXAdjust;
            RefreshTableHeader();
        }
    }//GEN-LAST:event_masterTableComponentMoved

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                SES130B dialog = new SES130B(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }




    @Override
    public void cmdRefreshAction() {
        super.cmdRefreshAction();
        
        /*
        java.util.Collection data = mitmasQuery.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        mitmasList.clear();
        mitmasList.addAll(data);        
         * 
         */
    }




    @Action
    public void comNavCloseAction() {
        cmdCloseAction();
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomNavJPanel;
    private javax.swing.JButton comNavClose;
    private javax.swing.JButton comNavNext;
    private javax.swing.JButton comNavPrev;
    private seso.RBgButton createSKURButton;
    private javax.persistence.EntityManager entityManager;
    private seso.RTextField fldMMITNO;
    private seso.RTextField fldMMITTY;
    private javax.swing.JLabel jLabel1;
    private seso.RPanel masterHeader;
    private javax.swing.JScrollPane masterJScrollPane;
    private javax.swing.JTable masterTable;
    private java.util.List<seso.Mitmas> mitmasList;
    private javax.persistence.Query mitmasQuery;
    private seso.RDialogDataAeraPanel rDlgDataAeraJPanel;
    private seso.RTableHeaderBackground rDlgTableHeaderBackground;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
